home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / FCOPY31.ARJ / STRTRIM.ASM < prev    next >
Assembly Source File  |  1992-04-19  |  7KB  |  126 lines

  1. ;-----------------------------------------------------------------------;
  2. ; STRTRIM.ASM                                                           ;
  3. ;                                                                       ;
  4. ; This module contains a C-callable function to remove leading and      ;
  5. ; trailing white space from a string.  The function is passed a         ;
  6. ; pointer to the string, and processes the string in place.  It         ;
  7. ; returns a pointer to the string that it processed.                    ;
  8. ;                                                                       ;
  9. ; This module recognizes a _model symbol, and can therefore             ;
  10. ; generate object code for any model desired.  If _model is not         ;
  11. ; defined, the SMALL model is used by default.                          ;
  12. ;                                                                       ;
  13. ;-----------------------------------------------------------------------;
  14. ; Function prototype:                                                   ;
  15. ;                                                                       ;
  16. ; char *strtrim (char *string)                                          ;
  17. ;-----------------------------------------------------------------------;
  18. ; Revision history:                                                     ;
  19. ;                                                                       ;
  20. ;       1.0     19 APR 92       Original.                               ;
  21. ;                                                                       ;
  22. ;                               Uses conditional assembly for all       ;
  23. ;                               memory models by defining _model.       ;
  24. ;                               Defaults to the small model if          ;
  25. ;                               _model is not defined.                  ;
  26. ;-----------------------------------------------------------------------;
  27. ;   Copyright (c) 1992 Ray Waters                                       ;
  28. ;   All Rights Reserved                                                 ;
  29. ;-----------------------------------------------------------------------;
  30.  
  31. IFDEF   _model                          ; if a _model was defined,
  32.         .MODEL  _model, C               ;  use it
  33. ELSE                                    ; else, default to
  34.         .MODEL  SMALL, C                ;  SMALL model
  35. ENDIF
  36.  
  37. IFDEF   ??version                       ; if using TASM,
  38.         LOCALS                          ;  enable local labels
  39. ENDIF
  40.  
  41. space   EQU     20h                     ; ASCII space or blank
  42. tab     EQU     9h                      ; ASCII tab
  43. lf      EQU     0Ah                     ; ASCII line feed
  44. vtab    EQU     0Bh                     ; ASCII vertical tab
  45. ff      EQU     0Ch                     ; ASCII formfeed
  46. cr      EQU     0Dh                     ; ASCII carriage return
  47.  
  48.         .CODE                           ; open code segment
  49.  
  50.         PUBLIC  strtrim                 ; make visible to Linker
  51. ;-----------------------------------------------------------------------;
  52. ; char *strtrim (char *string)                                          ;
  53. ;                                                                       ;
  54. ; Returns:  pointer to the string processed                             ;
  55. ;-----------------------------------------------------------------------;
  56. strtrim PROC    C USES si di, string:PTR BYTE
  57.  
  58. IF @DataSize                            ; if large data model,
  59.         push    ds                      ;  save original data segment
  60.         lds     si,[string]             ; load string address in DS:SI
  61. ELSE
  62.         mov     si,[string]             ; load string address in DS:SI
  63. ENDIF
  64.         mov     bx,si                   ; save pointer offset to string
  65.         mov     ax,ds                   ; set ES = DS
  66.         mov     es,ax
  67.         mov     di,si                   ; set DI = SI
  68.         cld                             ; strings forward
  69.  
  70. @@skip_space:
  71.         lodsb                           ; get a byte
  72.         cmp     al,space                ; is it a space?
  73.         je      @@skip_space            ; if yes, skip it
  74.         cmp     al,tab                  ; is it a tab
  75.         je      @@skip_space            ; if yes, skip it
  76.         cmp     al,lf                   ; is it a line feed?
  77.         je      @@skip_space            ; if yes, skip it
  78.         cmp     al,cr                   ; is is a carriage return?
  79.         je      @@skip_space            ; if yes, skip it
  80.         cmp     al,ff                   ; is it a formfeed?
  81.         je      @@skip_space            ; if yes, skip it
  82.         cmp     al,vtab                 ; is it a vertical tab?
  83.         je      @@skip_space            ; if yes, skip it
  84.         stosb                           ; else, copy it back to buffer
  85.         or      al,al                   ; is it the null byte?
  86.         jz      @@done                  ; if yes, finished
  87.  
  88. @@copy_chars:                           ; loop to copy rest of characters
  89.         lodsb                           ; get another character
  90.         stosb                           ; store the character
  91.         or      al,al                   ; was it the null character?
  92.         jnz     @@copy_chars            ; if not, loop until finished
  93.  
  94.         dec     di                      ; back DI up to null character
  95.         dec     di                      ;  and then the last string char
  96.         mov     si,di                   ; set SI = DI
  97.         std                             ; now skip white space in reverse
  98.  
  99. @@trim_trailing:
  100.         lodsb                           ; get a byte
  101.         cmp     al,space                ; is it a space?
  102.         je      @@trim_trailing         ; yes, keep going
  103.         cmp     al,tab                  ; is it a tab?
  104.         je      @@trim_trailing         ; yes, keep going
  105.         cmp     al,lf                   ; is it a line feed?
  106.         je      @@trim_trailing         ; yes, keep going
  107.         cmp     al,cr                   ; is it a carriage return?
  108.         je      @@trim_trailing         ; yes, keep going
  109.         cmp     al,ff                   ; is it a formfeed?
  110.         je      @@trim_trailing         ; yes, keep going
  111.         cmp     al,vtab                 ; is it a vertical tab?
  112.         je      @@trim_trailing         ; yes, keep going
  113.         mov     BYTE PTR [si+2],0       ; no, finished -- add terminating null
  114.  
  115. @@done:
  116. IF @DataSize                            ; if large data model,
  117.         mov     dx,ds                   ; return pointer segment in DX
  118.         pop     ds                      ; restore original data segment
  119. ENDIF
  120.         mov     ax,bx                   ; return pointer offset in AX
  121.         ret                             ; return to caller
  122.  
  123. strtrim ENDP
  124.  
  125.         END
  126.